#include "anime.h"

Anime::Anime() :
    QListWidgetItem(0,QListWidgetItem::Type)
{
    this->genres = QVector<QString>();
    this->episodes = QVector<Episode>();
    this->bonus = QVector<Episode>();
    this->loaded = false;

}

Anime::Anime(QString const& titre, QString const& fiche, const QString &image) :
    QListWidgetItem(0,QListWidgetItem::Type)
{
    this->genres = QVector<QString>();
    this->episodes = QVector<Episode>();
    this->bonus = QVector<Episode>();
    this->titre = titre;
    this->setText(titre);
    this->fiche = fiche;
    this->loaded = false;
    this->img_path = image;
}


QString& Anime::getTitre(){
    return this->titre;
}

QString& Anime::getDescription(){
    return this->description;
}
QString& Anime::getServeur(){
    return this->serveur;
}

QString& Anime::getTeam(){
    return this->team;
}

QString& Anime::getFiche(){
    return this->fiche;
}

int Anime::getEnLigne(){
    return this->enLigne;
}

int Anime::getTotal(){
    return this->Total;
}

int Anime::getId(){
    return this->id;
}

QString Anime::getGenres(){
    QString genres;
    int i;
    if(this->genres.size() >1)
        genres = "genres :";
    else
        genres = "genre :";

    genres += this->genres[0];
    for(i =1; i<this->genres.size(); i++)
        genres += ", "+this->genres[i];

    return genres;
}

QVector<Episode>& Anime::getEpisodes(){
    return this->episodes;
}

QVector<Episode>& Anime::getBonus(){
    return this->bonus;
}

void Anime::setTitre(QString const & titre){
    this->titre = titre;
    this->setText(titre);
}

void Anime::setDescription(const QString &description){
    this->description = description;
}

void Anime::setServeur(const QString& serveur){
    this->serveur = serveur;
}

void Anime::setTeam(const QString &team){
    this->team = team;
}

void Anime::setFiche(const QString& fiche){
    this->fiche = fiche;
}

void Anime::setEnLigne(int enLigne){
    this->enLigne = enLigne;
}

void Anime::setTotal(int total){
    this->Total = total;
}

void Anime::setId(int id){
    this->id = id;
}

void Anime::addGenre(QString const & genre){
    this->genres.append(genre);
}

void Anime::addEpisode(Episode const & episode){
    this->episodes.append(episode);
}

void Anime::addBonus(Episode const & bonus){
    this->bonus.append(bonus);
}


void Anime::load(){
    QString filename = QString("data/fiches/")+this->fiche+QString(".xml");
    QFile *file = new QFile(filename);

    if(file->exists()) {
        if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
            //emit afficherTexteLauncher(QString("Impossible de lire le fichier xml."));
            return;
        }

        QDomDocument doc("fiche");

        if(!doc.setContent(file)) {
            return;
        }

        QDomElement docElem = doc.documentElement();
        QDomNodeList children = docElem.childNodes();
        int i;

        QStringList myOptions;
        myOptions << "description" << "diffusion" << "createur" << "disponibles" << "totaux" << "team" << "serveur";

        for(i=0; i<children.count(); i++) {
            switch(myOptions.indexOf(children.item(i).nodeName())) {
                case 0:
                   //description
                   this->setDescription(children.item(i).toElement().text());
                   break;
                case 1:
                   //diffusion
                   // TODO : NOT IMPLEMENTED YET
                   break;
                case 2:
                   //createur
                    // TODO : NOT IMPLEMENTED YET
                   break;
                case 3:
                    //disponibles
                    this->setEnLigne(children.item(i).toElement().text().toInt());
                   break;
                case 4:
                    //totaux
                    this->setTotal(children.item(i).toElement().text().toInt());
                   break;
                case 5:
                    //team
                    this->setTeam(children.item(i).toElement().text());
                   break;
                case 6:
                    //serveur
                    this->setServeur(children.item(i).toElement().text());
                   break;
            }
        }

        int j;

        Episode myEpisode;
        QDomNode node;

        QDomNodeList liste_episodes = docElem.elementsByTagName("episode");
        for(i=0; i<liste_episodes.count(); i++) {
            node = liste_episodes.item(i);
            myEpisode = new Episode();
            myEpisode.load(&node);
            this->addEpisode(myEpisode);
        }

        QDomNodeList liste_bonus = docElem.elementsByTagName("fichier");
        for(i=0; i<liste_bonus.count(); i++) {
            node = liste_bonus.item(i);
            myEpisode = new Episode();
            myEpisode.load(&node);
            this->addBonus(myEpisode);
        }

       this->loaded = true;
    }
}

bool Anime::getLoad(){
    return this->loaded;
}

QString& Anime::getImage(){
    return this->img_path;
}
